<?php
/**
 * Copyright (c) 2008 Rob Levin. All rights reserved.
 *
 * This software is provided "as is," without warranty of any kind, express or implied. In no event 
 * shall the author or contributors be held liable for any damages arising in any way from the use 
 * of this software. 
 * 
 * This work is licensed under the Creative Commons Atrribution License. To view a copy of this license,
 * visit http://creativecommons.org/licenses/by/3.0/us/; or, (b) send a letter to Creative Commons, 
 * 171 2nd Street, Suite 300, San Francisco, California, 94105, USA.
 *
 *
 * Category:    Logging
 *
 * Description:
 * This is a simple class for logging. It allows for logging to syslog files,
 * custom files, and sending email for urgent errors (TBI). It also provides
 * a choice between xml and basic text logging for the custom logging function.
 * The system logs just uses text so as to not disturb the way the syslogs 
 * already work and look. 
 * 
 * LogLevels (set via LogConstants class):
 */
 require_once "LogConstants.php";
 
	class SimplePHPLogger {

	/**
	 * sendXMLToCustomLogFile: Log message to custom defined log file as xml. 
	 * 
	 * Description: static function for logging to a custom defined log file.
	 * 				The path/name should be dynamically loaded from the 
	 * 				LogConstants.php file as defined by: $CUSTOM_LOG_FILE. 
	 * 				This function will log the message as xml.
	 * 
	 * Parameters:
	 * 
	 * @param String $log_level 	
	 * 			String representing the log level.
	 * @param String $file	
	 * 			String representing the file your calling from. Just use: 
	 * 			__FILE__  on the call in.
	 * @param String $line
	 * 			String representing the line number from the calling script. Just use:
	 * 			__line__ on the call in.
	 * @param String $err
	 * 			String representing the error message (I guess it doesn't HAVE 
	 * 			to be an error message if you're logging for information)
	 * @return void
	 */
		public static function sendXMLToCustomLogFile($log_level, $file, $line, $err="") {
			if(LogConstants::$CURRENT_LOG_LEVEL >= $log_level) {
				error_log(
							"<br />\n\n<b>Time</b>: ".  self::getTimeStampNow() . 
							"<br />\n<b>Log Level</b>: " . LogConstants::levelToString($log_level) . 
							"<br />\n<b>Error</b>: " . $err .
							"<br />\n<b>File</b>: " . $file . 
							"<br />\n<b>Line Number</b>: " . $line . 
							"<br />\n", 
					LogConstants::$FILE_LOG, 
					LogConstants::$CUSTOM_LOG_FILE);
			}
		}

	/**
	 * sendTextToCustomLogFile: Log message to custom defined log file as text. 
	 * 
	 * Description: static function for logging to a custom defined log file.
	 * 				The path/name should be dynamically loaded from the 
	 * 				LogConstants.php file as defined by: $CUSTOM_LOG_FILE. 
	 * 				This function will log the message as plain text.
	 * 
	 * Parameters:
	 * 
	 * @param String $log_level 	
	 * 			String representing the log level.
	 * @param String $file	
	 * 			String representing the file your calling from. Just use: 
	 * 			__FILE__  on the call in.
	 * @param String $line
	 * 			String representing the line number from the calling script. Just use:
	 * 			__line__ on the call in.
	 * @param String $err
	 * 			String representing the error message (I guess it doesn't HAVE 
	 * 			to be an error message if you're logging for information)
	 * @return void
	 */
		public static function sendTextToCustomLogFile($log_level, $file, $line, $err="") {
			if(LogConstants::$CURRENT_LOG_LEVEL >= $log_level) {
				error_log("\n\n" . 
						self::getTimeStampNow() . 
						"\nLog Level: " . LogConstants::levelToString($log_level) . 
						"\nMessage:  " . $err .
						"\nFile: " . $file . "  " .
						"\nLine Number: " . $line . "  ", 
					LogConstants::$FILE_LOG, 
					LogConstants::$CUSTOM_LOG_FILE);
			}
		}
		
	/**
	 * sendLogToSystemLog: Log system message to default system log file (apache usually): 
	 * 
	 * Description: static function for sending a log message to the default
	 * 				log file. For apache served web pages should be the apache
	 * 				log file. ie usr/local/apache/logs/project_name if set up
	 * 				by Newman, or I believe the default might otherwise be:
	 * 				/var/log/apache2/error.log This logs as plain text.
	 * 
	 * Parameters:
	 * 
	 * @param String $log_level 	
	 * 			String representing the log level.
	 * @param String $file	
	 * 			String representing the file your calling from. Just use: 
	 * 			__FILE__  on the call in.
	 * @param String $line
	 * 			String representing the line number from the calling script. Just use:
	 * 			__line__ on the call in.
	 * @param String $err
	 * 			String representing the error message (I guess it doesn't HAVE 
	 * 			to be an error message if you're logging for information)
	 * @return void
	 */
		public static function sendLogToSystemLog($log_level, $file, $line, $err="") {
			if(LogConstants::$CURRENT_LOG_LEVEL >= $log_level)
				error_log( self::getTimeStampNow() . 
					" Log Level: " . LogConstants::levelToString($log_level) . 
					" Message:  ".$err."  File: " . $file . 
					" Line Number: " . $line . "  ", 
				LogConstants::$SYSTEM_LOG
				);
		}
		
	/**
	 * sendEmailLogMessage: Log message emailing it to the 'to' email address
	 * 						 
	 * 
	 * Description: static function for sending a log message to the email
	 * 				address defined in LogConstants::$EMERG_EMAIL_TO property.
	 * 				From the $EMERG_EMAIL_FROM property.
	 * 
	 * Parameters:
	 * 
	 * @param String $log_level 	
	 * 			String representing the log level.
	 * @param String $file	
	 * 			String representing the file your calling from. Just use: 
	 * 			__FILE__  on the call in.
	 * @param String $line
	 * 			String representing the line number from the calling script. Just use:
	 * 			__line__ on the call in.
	 * @param String $err
	 * 			String representing the error message (I guess it doesn't HAVE 
	 * 			to be an error message if you're logging for information)
	 * @return void
	 */
		public static function sendEmailLogMessage($log_level, $file, $line, $err="") {
			if(LogConstants::$CURRENT_LOG_LEVEL >= $log_level) {

				error_log("\n-- Error Logged -- \nTime thrown: " . self::getTimeStampNow() . 
						"\nLog Level: " . LogConstants::levelToString($log_level) . 
						"\nError Message: " . $err,
						LogConstants::$MAIL_LOG,
						LogConstants::$EMERG_EMAIL_TO,
						LogConstants::$EMERG_EMAIL_FROM);
			}
		}
		
		
		// Get a timestamp in the following form: March 31 15:46:15
		private static function getTimeStampNow () {
			$today = getdate();
			return 	$today['month'] . ' ' . 
					$today['mday'] . ' ' . 
					$today['hours'] . ':' . 
					$today['minutes'] . ':' . 
					$today['seconds'];
		}
		
	}	// Ends class
	
?>
